import 'package:dsp48_v10/src/search/search_item.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

class SearchView extends StatefulWidget {
  const SearchView({super.key});

  @override
  State<SearchView> createState() => _SearchViewState();
}

class _SearchViewState extends State<SearchView> implements BLESearchTableViewDelegate {
  BLESearchTableView? _tableView;
  late String _date;
  @override
  void initState() {
    super.initState();
    DateTime now = DateTime.now();
    String year = now.year.toString();
    String month = now.month.toString().padLeft(2, '0');
    String day = now.day.toString().padLeft(2, '0');
    _date = '${year}-${month}-${day}';
    _tableView = BLESearchTableView(
        filterName: const ['DSP'],
        agreement: this,
        contentColor: Colors.white,
        bgViewForHeader: (child) => Container(color: Colors.transparent, child: child),
        bgViewForFooter: (child) => Container(color: Colors.transparent, child: child));
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      backgroundColor: Colors.transparent,
      child: Stack(
        children: [
          InkWell(
            splashColor: Colors.transparent,
            onTap: () => Navigator.pop(context),
          ),
          Padding(
            // padding: const EdgeInsets.only(top: kToolbarHeight),
            padding: const EdgeInsets.all(0),
            child: Container(
                decoration: const BoxDecoration(
                  color: Colors.black, // 背景色
                  borderRadius:
                      BorderRadius.only(topLeft: Radius.circular(16), bottomLeft: Radius.circular(16)), // 设置圆角
                ),
                child: _tableView),
          ),
        ],
      ),
    );
  }

  @override
  List<RowModel> listModelForHeader(String appVersion) {
    return ['Devices'].map(($0) {
      return RowModel(
          text: $0,
          height: 60,
          bgColor: Colors.transparent,
          lineColor: Colors.white,
          lineHeight: 1,
          textStyle: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold));
    }).toList();
  }

  @override
  List<RowModel> listModelForFooter(String appVersion) {
    return ['Refresh Device List', /*'Reset DSP Tuning'*/ '${_date}'].map(($0) {
      return RowModel(text: $0, height: 40, bgColor: Colors.transparent, lineColor: Colors.white, lineHeight: 1);
    }).toList();
  }

  @override
  List<RowModel> listModelForSection(List<String> list) {
    return [];
  }

  @override
  void methodForClickBLESearchTableView(BLESearchTableViewClickType type, String tag) {
    switch (type) {
      case BLESearchTableViewClickType.header:
        break;
      case BLESearchTableViewClickType.footer:
        switch (tag) {
          case 'Refresh Device List':
            _tableView?.refreshKey?.currentState?.show();
            break;
          case 'Reset DSP Tuning':
            showAlert(context, 'Are you sure to reset DSP tuning?', ['NO', 'YES'], (value) {
              if (value == 'YES') {
                globalController.resetDSP();
              }
            });
            break;
        }
        break;
      case BLESearchTableViewClickType.section:
        break;
      default:
    }
  }

  @override
  Widget viewForRowInSection(DSPeripheral peripheral, int section, int row, bool isOn) {
    return SearchItem(
      peripheral: peripheral,
      onClick: () => _tableView?.clickDevice(section, row),
      onLongPress: () => _tableView?.changeDeviceName(section, row, peripheral.peripheral, callBack: (name) {
        globalController.updateBluetoothName(name);
      }),
    );
  }

  ///修改设备名称
  // void changeDeviceName() {
  //   for (var i = 0; i < (_tableView?.isonList ?? []).length; i++) {
  //     List<bool> list = _tableView!.isonList![i];
  //     for (var j = 0; j < list.length; i++) {
  //       bool isconnect = list[j];
  //       if (isconnect) {
  //         DSPeripheral peripheral = _tableView!.deviceList[i].values.first[j];
  //         _tableView?.changeDeviceName(i, j, peripheral.peripheral,callBack: (name) {
  //           globalController.updateBluetoothName(name);
  //         });
  //       }
  //     }
  //   }
  // }
}
